package com.nobuyme.box.toy;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nobuyme.box.R;
import com.nobuyme.box.app.UrlCenter;
import com.nobuyme.box.bean.ShareListEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeGridFragment;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;


/**
 * A simple {@link Fragment} subclass.
 */
public class ToyShareFragment extends SwipeGridFragment {
    int flag;
    private int mPage = 0;

    public ToyShareFragment() {
        super(5);
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setTriggerLoadItemCount(4);
        clearDecorations();
        setPullToRefreshEnable(false);
        startLoading();
        return view;
    }

    public static ToyShareFragment newInstance(int flag) {
        Bundle args = new Bundle();
        args.putInt(ToyIntroduceActivity.KEY_FLAG, flag);
        ToyShareFragment fragment = new ToyShareFragment();
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        List<ShareListEntity> data = (List<ShareListEntity>) getAdapter().getData();
        String content = data.get(position).getContent();
        ArrayList<String> image_list = (ArrayList<String>) data.get(position).getImage_list();
        ShareDetailActivity.start(content,image_list,getActivity());
    }


    @Override
    public RecyclerView.LayoutManager createLayoutManager() {
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL);
        return staggeredGridLayoutManager;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new ToyShareListAdapter(getActivity());
    }

    private ListDataRequestListener<ShareListEntity> mResponceListener = new ListDataRequestListener<>(this, ShareListEntity.class);

    @Override
    public void onStartLoading() {
        flag = getArguments().getInt(ToyIntroduceActivity.KEY_FLAG);
        String urlShareList = String.format(UrlCenter.SHARE_LIST, flag);
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
            IHttpRequest request = new HttpRequest(getContext());
            UrlParams params = new UrlParams();
            params.put("pageNo", mPage + "");
            params.put("pageSize", 12 + "");
            params.put("goodsId", flag + "");

            request.get(urlShareList, params, mResponceListener);
        }

    }

    public class ToyShareListAdapter extends RecyclerViewBaseAdapter<ShareListEntity> {

        ToyShapeHolder holder;
        public ToyShareListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new ToyShapeHolder(setAdapterViewItem(parent, R.layout.share_list_item));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder ItemHolder, int position) {
            ShareListEntity shareListEntity = getData().get(position);

            holder = (ToyShapeHolder) ItemHolder;
            holder.mTvShareTitle.setText(shareListEntity.getContent());
            if(shareListEntity.getImage_list().size()==0){
                holder.mIvSharePic.setVisibility(View.GONE);
            }else{
                Glide.with(getActivity()).load(shareListEntity.getImage_list().get(0)).into(holder.mIvSharePic);
            }
            Glide.with(getActivity()).load(shareListEntity.getAvatar()).into(holder.mIvShareHead);
             }

        }

    class ToyShapeHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_share_title)
        TextView mTvShareTitle;
        @Bind(R.id.iv_share_pic)
        ImageView mIvSharePic;
        @Bind(R.id.iv_share_head)
        CircleImageView mIvShareHead;
        @Bind(R.id.tv_share_name)
        TextView mTvShareName;
        public ToyShapeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
